package cn.train.controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Time;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.train.model.Carpiao;
import cn.train.model.Cartable;
import cn.train.service.EditTrainServices;
@WebServlet("/EditTrainEditServlet")
public class EditTrainEditServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uid=req.getParameter("uid");
		String carid=req.getParameter("carid");
		int m=Integer.parseInt(uid);
		int n=Integer.parseInt(carid);
		String name=req.getParameter("cname");
		String type=req.getParameter("ctype");
		String actname=req.getParameter("actname");
		String acttime= req.getParameter("acttime");
		String overname= req.getParameter("overname");
		String overtime= req.getParameter("overtime");
		String peice= req.getParameter("price");
		String pcount= req.getParameter("pcount");
		int num=Integer.parseInt(pcount);
		System.out.println(name+type+peice);
		BigDecimal bigNum = new BigDecimal(peice).setScale(2,BigDecimal.ROUND_HALF_UP);
		
		Time gotime=Time.valueOf(acttime);
		Time overt=Time.valueOf(overtime);

		Carpiao cp=new Carpiao();
		cp.setCpid(m);
		cp.setGoname(actname);
		cp.setGotime(gotime);
		cp.setOvername(overname);
		cp.setOvertime(overt);
		cp.setPnum(num);
		cp.setJienjia(bigNum);
		Cartable cb=new Cartable();
		cb.setCarid(n);
		cb.setCrname(name);
		cb.setCtype(type);
		cp.setCartable(cb);
		EditTrainServices ets=new EditTrainServices();
		int nums=ets.update(cp);
		resp.setContentType("text/html;charSet=utf-8");
		PrintWriter out =resp.getWriter();
		String flag=req.getParameter("flag");
		if(flag!=null){
		if(nums>0){
			out.print("<script>alert('修改成功！');location.href='EditTrainServlet'</script>;");
		}else{
			out.print("<script>alert('修改失败');location.href='EditTrainServlet'</script>;");
		}
		}else{
			if(nums>0){
				out.print(1);
			}else{
				out.print(0);
			}
		}
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		doGet(req, resp);
	}
}
